package cn.hcbycloud.common.jwt.config;


import cn.hcbycloud.common.jwt.properties.EncryptionKeyProperties;
import cn.hcbycloud.common.jwt.service.encryptionkey.BaseGenerateJwtKeyService;
import cn.hcbycloud.common.jwt.service.encryptionkey.RsaEncryptionKeyCacheService;
import cn.hcbycloud.common.jwt.service.encryptionkey.RsaEncryptionKeyService;
import cn.hcbycloud.common.jwt.service.encryptionkey.impl.GenerateJwtKeyServiceImpl;
import cn.hcbycloud.common.jwt.service.encryptionkey.impl.RsaEncryptionKeyServiceImpl;
import cn.hcbycloud.common.jwt.service.jwt.JwtBuilderFactory;
import cn.hcbycloud.common.jwt.service.jwt.JwtRedisCacheService;
import cn.hcbycloud.common.jwt.service.jwt.JwtService;
import cn.hcbycloud.common.jwt.service.jwt.impl.JwtBuilderServiceImpl;
import cn.hcbycloud.common.jwt.service.jwt.impl.JwtRedisCacheServiceImpl;
import cn.hcbycloud.common.jwt.service.jwt.impl.JwtServiceImpl;
import cn.hcbycloud.common.redis.utils.RedisOperator;
import lombok.AllArgsConstructor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;

/**
 * @author gaoh
 * @desc 通用配置组件包配置类
 * @date 2022年01月12日 下午 5:07
 */
@Configuration
@AllArgsConstructor
public class CommonConfig {
    EncryptionKeyProperties encryptionKeyProperties;
    RedisOperator redisOperator;

    /**
     * 注入密钥存取内存服务
     *
     * @return
     */
    @Bean(name = "rsaEncryptionKeyCacheService")
    @DependsOn({"rsaEncryptionKeyService"})
    public RsaEncryptionKeyCacheService rsaEncryptionKeyCacheService(RsaEncryptionKeyService rsaEncryptionKeyService) {
        return new RsaEncryptionKeyCacheService(rsaEncryptionKeyService);
    }

    /**
     * 注入密钥对生成服务
     *
     * @return
     */
    @Bean(name = "rsaEncryptionKeyService")
    public RsaEncryptionKeyService rsaEncryptionKeyService() {
        return new RsaEncryptionKeyServiceImpl();
    }

    /**
     * JWT相关服务接口
     *
     * @return
     */
    @Bean
    public JwtService jwtService() {
        return new JwtServiceImpl();
    }

    /**
     * 默认实现组装JWTtoken
     * 默认实现bean,该bean在单体项目中可自定义实现
     */
    @Bean(name = "jwtBuilderService")
    @ConditionalOnMissingBean(JwtBuilderFactory.class)
    public JwtBuilderFactory getJwtBuilderFactory() {
        return new JwtBuilderServiceImpl();
    }

    /**
     * 默认获取密钥对字符串接口
     * 默认实现bean,该bean在单体项目中可自定义实现
     */
    @Bean(name = "generateJwtKeyService")
    @ConditionalOnMissingBean(BaseGenerateJwtKeyService.class)
    public BaseGenerateJwtKeyService getBaseGenerateJwtKeyService() {
        return new GenerateJwtKeyServiceImpl(encryptionKeyProperties);
    }

    /**
     * 默认实现JWTredis缓存配置
     * 默认实现bean,该bean在单体项目中可自定义实现
     */
    @Bean(name = "jwtRedisCacheService")
    @ConditionalOnMissingBean(JwtRedisCacheService.class)
    public JwtRedisCacheService getJwtRedisCacheService() {
        return new JwtRedisCacheServiceImpl(redisOperator);
    }
}